/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package abricots.entity.controller;

import abricots.entity.PlayerEntity;
import abricots.game.GameEnv;
import java.util.HashMap;
import java.util.Map;

/**
 *
 * @author charly
 */
public class KeyboardController {
   protected Map<Integer, Integer> keyCodeToCommandMap;
   private PlayerEntity entity;
   private GameEnv env;

   public KeyboardController(PlayerEntity entity, GameEnv env) {
      keyCodeToCommandMap = new HashMap<Integer, Integer>();
      this.entity = entity;
      this.env = env;
   }

   public void bind(int commandId, int keyId) {
      keyCodeToCommandMap.put(keyId, commandId);
   }

   public void clearBinds() {
      keyCodeToCommandMap.clear();
   }

   public void keyPressed(int keyCode) {
      if (!keyCodeToCommandMap.containsKey(keyCode)) {
         return;
      }
      int commandId = keyCodeToCommandMap.get(keyCode);
      entity.processCommand(commandId, true);
   }

   public void keyReleased(int keyCode) {
      if (!keyCodeToCommandMap.containsKey(keyCode)) {
         return;
      }
      int commandId = keyCodeToCommandMap.get(keyCode);
      entity.processCommand(commandId, false);
   }
}
